<template>
	<view>
		<div>
			<navitop>
				<i class="iconfont" slot="left-slot" @click="outlot">&#xe667;</i>
				<span slot="center-slot">编辑客户信息</span>
				<!-- <i class="iconfont " slot="right-slot" @click="edit()"></i> -->
			</navitop>
		</div>
		<scroll-view class="staff_b" scroll-y="true" touch-action=none; >
		<div class="detial">
			<div :style="'margin-top:' + nv_height"></div>
			<div class="public_list_out" v-for="(item,index) in results">
				<div class="public_list" v-if="item.code !== 'crmNo' && item.code !== 'areaId' && item.dataType == '1' && item.type == '文本'">
					<div class=" public_list_left_one">
						<text v-if="item.onAppFill == 1" style="color: red; ">*</text>
						<text>{{item.name}}:</text>
					</div>
					<div class="public_list_right">
						<input type="text" :ref="item.username" v-model="item.value"  >
					</div>
				</div>
				<div class="public_list"  v-else-if="item.code == 'areaId'">
					<div class=" public_list_left_one">
						<text v-if="item.onAppFill == 1" style="color: red; ">*</text>
						<text>{{item.name}}:</text>
					</div>
					<div class="public_list_right">
						<input @click="alertcity" type="text" :ref="item.username" v-model="item.value"  >
					</div>
				</div>
				<div class="public_list"  v-else-if="item.dataType == '2'">
					<div class=" public_list_left_one">
						<text v-if="item.onAppFill == 1" style="color: red; ">*</text>
						<text>{{item.name}}:</text>
					</div>
					<div class="public_list_right">
						<input type="text" :ref="item.username" v-model="item.values"  >
					</div>
				</div>
				<div class="public_list" v-else-if="item.type == '时间' || item.type == '日期'" @click="itme_code(item.code)">
					<div class=" public_list_left_one">
						<text v-if="item.onAppFill == 1" style="color: red; ">*</text>
						<text>{{item.name}}:</text>
					</div>
					<div class="public_list_right">
						<text @click="time_altert(item.value)" v-if="item.value">{{item.value}}</text>
						<text @click="time_altert(item.value)" v-else>选择时间</text>
					</div>	
				</div>
				<div class="public_list" v-else-if="item.type == '下拉' ">
					<div class=" public_list_left_one">
						<text v-if="item.onAppFill == 1" style="color: red; ">*</text>
						<text>{{item.name}}:</text>
					</div>
					<div class="public_list_right">
						<input type="text" @click="drop_duon(item.code)" disabled='true' v-model="item.value">
					</div>
				</div>
				<div class="public_list" v-else-if="item.type == '搜索选择' ">
					<div class=" public_list_left_one">
						<text v-if="item.onAppFill == 1" style="color: red; ">*</text>
						<text>{{item.name}}:</text>
					</div>
					<div class="public_list_right">
						<input type="text" @click="drop_duo(item.code)" disabled='true' v-model="item.value">
					</div>
				</div>
			</div>
			
		</div>
		</scroll-view>
		<div :style="heigsdiv"></div>
		<div class="public_button" :style="heigs" v-show="buttom_show">
			<div class="public_text" @click="edit">
				保&nbsp;&nbsp;存
			</div>
		</div>
		<!-- 弹窗组件 -->
		<popus ref="alerts">
			<alertcenter @row="row" @closes="closes" :dataSorc="data" ></alertcenter>
		</popus>
		
		<popus ref="aler">
			<kxTime @rundata='lasttime' @cloes="cloes" :start='1' :date="data" :default_time="1"></kxTime>
		</popus>
		
		<ztree ref='alert' @cityalerts="cityalerts" :dataSorce="citys_c"></ztree>
	</view>
</template>

<script> 
	import navitop from '@/components/common/navi-top'
	import alertcenter from '@/pages/common/common/alert/alertcenter'
	import popus from '@/uni_modules/uni-popup/components/uni-popup/uni-popup'
	import kxTime from "@/components/kx-datetime/kx-datetimes"
	import ztree from '@/pages/common/common/city/z_citys.vue'
	import {detail,save,namelist,city,list,dataList} from '@/common/api/job/jobapi.js'
	import {droplist} from '@/common/api/job/alert.js'
	import {Z_threes} from '@/common/api/common/public_api.js'
	export default {
		components:{
			navitop,
			popus,
			alertcenter,
			ztree,
			kxTime
		},
		data(){
			return {
				dataSorce: [],
				results: [],
				uid: '',
				data: '',
				drop_down: [],
				drop_down_list: [],
				index: '',
				city: [],
				url: '',
				citys_c: [],
				heigs:'',
				heigsdiv: 0,
				date: '', //需要展示的时间数据
				enddate: "2200", // 时间段的终止时间，不设置默认当前之间终止
				startdate: "2021" ,//时间段的开始时间，不设置默认计算机最初时间
				Time_code: '',	//获取时间code	
				dataLi: [],		//获取部门
				public_name: []	,// 搜索选择
				
				buttom_show: false,
				
				nv_height:'',		// 设置顶部导航高度
				
			}
		},
		onLoad(option) {
			this.uid = option.id;
			this.url = '&crmType=' + option.crmType + '&name=' + option.name + '&classid=' + option.classid + '&index=' + option.index
			this.listdata();
			this.citys();
			this.dataLis();
			uni.onKeyboardHeightChange(res => {				res.height = res.height + 50;
				this.heigsdiv = 'height:' + res.height  + 'px;' ;
				this.heigs = 'height:' + res.height + 'px;' ;
			})
			
			this.nv_height = uni.getSystemInfoSync().statusBarHeight + 75 + 'px';
			
		},
		methods:{
			
			time_altert(val){
				this.data = val;
				this.$refs.aler.open('bottom')
			},
			
			cloes(){
				this.$refs.aler.close()
			},
			
			// 时间
			lasttime(e){
				this.$refs.aler.close()
				this.results.forEach(item => {
					if(item.code == this.Time_code){
						this.$set(item,'value',e)
					}
				})
			},
			// 获取时间code
			itme_code(val){
				this.Time_code = val;
			},
			
			// 返回
			outlot(){
				uni.navigateTo({
					url: '/pages/common/index/detial?id=' + this.uid + this.url 
				});
			},
			
			// 地区选择
			alertcity(){
				this.$refs.alert.z_three();
			},
			
			cityalerts(val){
				for(let i in this.results){
					if(this.results[i].code == 'areaId'){
						this.results[i].value = val;
					}
				}
			},
			
			citys(){
				let children = [];
				city({current: 1,size: -1}).then((result)=> {
					this.citys_c = Z_threes(result.data,[])
				})
			},
			
			
			
			//下拉
			drop_duon(val){
				let rst = [];
				for(let i in this.results){
					if(val == this.results[i].code){
						rst = droplist(this.results[i],val);
					}
				}
				this.data = rst;
				this.$refs.alerts.open('bottom')
			},
			
			drop_duo(val){
				if(val == 'officeId'){
					this.dataLi.forEach(item => {
						this.$set(item,'code',val)
					})
					this.data = this.dataLi;
				}else{
					this.public_name.forEach(item => {
						this.$set(item,'code',val)
					})
					this.data = this.public_name;
				}
				this.$refs.alerts.open('bottom')
			},
			
			
			row(val){
				this.results.forEach(item => {
					if(item.code == val.code){
						this.$set(item,'value',val.name)
					}
				})
					this.$refs.alerts.close();
			},
			
			// 获取部门
			dataLis(){
				dataList({isLoadUser: 2}).then((rows) => {
					if(rows.code == 200){
						this.public_name = rows.data;
						for(let i in rows.data){
							if(rows.data[i].parentId !== '0' && rows.data[i].id.split("_")[1] !== 'user'){
								this.dataLi.push(rows.data[i]);
							}
						}
					}
				})
			},
			// 自定义字段
			listdata(){
				list({source: 'crm'}).then((rows) => {
					if (rows.code === 200) {
						detail({id: this.uid}).then((result) => {
							if (result.code == 200) {
								if(result.data.defineFields !== '' && result.data.defineFields !== null && result.data.defineFields !== undefined){
									result.data.defineFields = JSON.parse(result.data.defineFields);
								}
								rows.data.forEach(item => {
									if (item.code.indexOf('Id') != '-1') {
										let tempname = item.code.substring(0,item.code.length - 2) + 'Name';
										this.$set(item, 'value', result.data[tempname])
									}else{
										this.$set(item, 'value', result.data[item.code])
										if(result.data.defineFields !== '' && result.data.defineFields !== null && result.data.defineFields !== undefined){
											this.$set(item, 'values', result.data.defineFields[item.id])
										}
									}
								})
								this.results = rows.data;
								if(this.results){
									this.buttom_show = true;
								}
							}
						})
					}
				})
			},
			
			// 修改		
			edit(){
				let uname = {};
				let define = {};
				let defineFields = '';
				for(let i in this.results){
					if(this.results[i].onAppFill){
						if(this.results[i].value){
							if( this.results[i].dataType == 1){
								if (this.results[i].code.indexOf('Id') != '-1') {
									let tempname = this.results[i].code.substring(0,this.results[i].code.length - 2) + 'Name';
									uname[tempname] = this.results[i].value
								}else{
									uname[this.results[i].code] = this.results[i].value
								}
							}else{
								define[this.results[i].id] = this.results[i].values
							}
						}else{
							uni.showToast({
								title: '请输入'+this.results[i].name,
								icon: 'error'
							})
							return false;
						}
					}else{
					
						if( this.results[i].dataType == 1){
							if (this.results[i].code.indexOf('Id') != '-1') {
								let tempname = this.results[i].code.substring(0,this.results[i].code.length - 2) + 'Name';
								uname[tempname] = this.results[i].value
							}else{
								uname[this.results[i].code] = this.results[i].value
							}
						}else{
							define[this.results[i].id] = this.results[i].values
						}
					
					}
				}
				uname['id'] = this.uid;
				uname['defineFields'] = JSON.stringify(define)
				save(uname).then((rst) => {
					if(rst.code == 200){
						uni.showToast({
						    title: '修改成功！',
						});
						setTimeout(() => {
							uni.navigateTo({
									url: '/pages/common/index/detial?id=' + this.uid + this.url 
							});
						},1500)
					}
				})
			}
		}
	}
	
</script>

<style scoped lang="scss">

</style>
